package com.bupt.secret;


import com.alibaba.fastjson2.JSONObject;
import com.bupt.secret.constant.AlgorithmConstants;
import com.bupt.secret.dto.*;
import com.bupt.secret.utils.BuptClientUtil;
import com.bupt.secret.utils.BuptResultDto;

/**
 * @author ShiShuMin
 * @version v1.0
 * @ClassName BuptSecretApplication
 * @Date: 2024/8/24 11:54
 * @Description:
 */
public class BuptSecretClientApplication {

    public static void main(String[] args){

        BuptResultDto buptResultDto = BuptClientUtil.openSession();
        Object data = buptResultDto.getData();
        Long session = (Long)data;

//        DeviceInfo deviceInfo = BuptClientUtil.getDeviceInfo(session);
//        System.out.println("deviceinfo：" + JSONObject.toJSONString(deviceInfo));
//
////        BuptClientUtil.generateKeyWithIPKRSA(session, 1, 128);
//
//        // 获取随机数
//        BuptResultDto ramdom = BuptClientUtil.generateRandom(session, 4);
//        System.out.println("ramdom:"+ JSONObject.toJSONString(ramdom));
//
       /* BuptResultDto<RSArefPublicKey> encPublicKeyRSA = BuptClientUtil.exportEncPublicKeyRSA(session, 1);
        System.out.println("encPublicKeyRSA:" + JSONObject.toJSONString(encPublicKeyRSA));
//        {"autoRead":true,"autoWrite":true,"bits":2048,"e":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAQ==","m":"0tvY2o7DsYkMLiWEkWRU4au4XKGiVMTT2SG9B2yeWPl1p0wpasZyRvjrX9gYpe4PeBzPSvhEvcq991hdsuXXEyJCr7d5ZOiJIo4j1Zsx8VDTIMeqOiIYnVeCg1p/rNCP50gFOrqEu9L4lw6dohTHbmAE1fHeSM8QskLSF60jqCIQs+pc3h4k43C92WjWqvZEepxjXr3k/hc2OEGyFLL+BmY1DxVqXsVsWv4Jl89wnqmf96T4DLPGhQPJGegbVcJjV/UkFVQj4qgRKee+6oNYr0sJmN55rKSFYTY65ZEWP/p8N2jyBcIwBscdD1fpV0XYgcCARI3P1KsIfedmpjA/jQ==","pointer":{}}

//        BuptResultDto<RSArefCommonKeyBo> commonKey = BuptClientUtil.generateKeyPairRSA(session, 2048);
//        System.out.println("commonKey:" + JSONObject.toJSONString(commonKey));
////
        BuptResultDto<RSASessionKeyBo> rsaSessionKeyBoBuptResultDto = BuptClientUtil.generateKeyWithEPKRSA(session, 1, encPublicKeyRSA.getData());
        System.out.println("sessionKey:" + JSONObject.toJSONString(rsaSessionKeyBoBuptResultDto));
////
        long phKeyHandle = rsaSessionKeyBoBuptResultDto.getData().getPhKeyHandle();
        String plain = "dfdfsaf1231231231221432";
        byte[] iv = "1234567812345678".getBytes();

        BuptResultDto<EncDataBo> encrypt = BuptClientUtil.encrypt(session, phKeyHandle, AlgorithmConstants.SGD_SMS4_CBC, iv, plain);
        System.out.println("encrypt:"+JSONObject.toJSONString(encrypt));


         plain = "{\"dfdfsaf1231231231221432\":1231}";

         encrypt = BuptClientUtil.encrypt(session, phKeyHandle, AlgorithmConstants.SGD_SMS4_CBC, iv, plain);
        System.out.println("encrypt:"+JSONObject.toJSONString(encrypt));

        plain = "{\"对吧123\":\"?2123是打发第三方$#$@??\"}";

        encrypt = BuptClientUtil.encrypt(session, phKeyHandle, AlgorithmConstants.SGD_SMS4_CBC, iv, plain);
        System.out.println("encrypt:"+JSONObject.toJSONString(encrypt));*/


        BuptResultDto<RSArefCommonKeyBo> rsArefCommonKeyBoBuptResultDto = BuptClientUtil.generateKeyPairRSAV2(session, 2048,"123321", 2049);
        System.out.println("rsArefCommonKeyBoBuptResultDto:"+JSONObject.toJSONString(rsArefCommonKeyBoBuptResultDto));


        RSArefPublicKey rsArefPublicKey = rsArefCommonKeyBoBuptResultDto.getData().getRsArefPublicKey();
        CipherRSArefPrivateKey rsArefPrivateKey = rsArefCommonKeyBoBuptResultDto.getData().getRsArefPrivateKey();
        BuptResultDto status = BuptClientUtil.importKeyPairRSAV2(session, 2048,"123321", 2049, rsArefPublicKey, rsArefPrivateKey);
        System.out.println("status:"+JSONObject.toJSONString(status));

        BuptClientUtil.closeSession(session);
    }



}
